package org.springblade.modules.repair.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tenant.annotation.TenantIgnore;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.repair.dto.*;
import org.springblade.modules.repair.service.IMaintainPlanContentService;
import org.springblade.modules.repair.service.MaintainPlanUserDayCountService;
import org.springblade.modules.repair.vo.*;
import org.springblade.modules.repair.vo.plan.v2.PlanUserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Date 13:58 2021/5/24
 * @Description {
 * <p>
 * }
 * @Author lsay
 **/
@RestController
@RequestMapping("/repairCheck")
@Api(value = "工单信息相关接口", tags = "工单信息相关接口")
public class RepairCheckController {

	@Autowired
	private IMaintainPlanContentService planContentService;
	@Autowired
	private MaintainPlanUserDayCountService planUserDayCountService;

	/**
	 * 获取保养统计
	 *
	 * @param userIds
	 * @return
	 */
	@TenantIgnore
	@PostMapping("/plan/check")
	public R<CheckPlanCountVO> planCheck(@RequestParam("userIds") String userIds, @RequestParam(value = "startTime") String startTime, @RequestParam("endTime") String endTime) {
		return R.data(planContentService.checkPlanCount(Func.toLongList(userIds), startTime, endTime));
	}

	/**
	 * 保养统计列表跳转
	 *
	 * @param queryDTO
	 * @param query
	 * @return
	 */
	@TenantIgnore
	@PostMapping("/plan/list")
	public R<IPage<PlanDataPageVO>> planList(@RequestBody CheckPlanPageQueryDTO queryDTO, @RequestBody Query query) {
		return R.data(planContentService.selectPlanList(queryDTO, Condition.getPage(query)));
	}

	/**
	 * 评分统计列表跳转
	 *
	 * @param queryDTO
	 * @param query
	 * @return
	 */
	@TenantIgnore
	@PostMapping("/score/list")
	public R<IPage<PlanDataPageVO>> scoreList(@RequestBody CheckPlanPageQueryDTO queryDTO, @RequestBody Query query) {
		return R.data(planContentService.selectScoreList(queryDTO, Condition.getPage(query)));
	}

	/**
	 * 保养统计列表统计
	 *
	 * @param queryDTO
	 * @return
	 */
	@TenantIgnore
	@PostMapping("/plan/count")
	public R<Long> checkPlanCount(@RequestBody CheckPlanPageQueryDTO queryDTO) {
		return R.data(planContentService.selectCheckPlanCount(queryDTO));
	}

	/**
	 * 电梯历史列表
	 *
	 * @param queryDTO
	 * @param query
	 * @return
	 */
	@TenantIgnore
	@PostMapping("/elevator/plan/history")
	public R<?> elevatorPlanHistory(@RequestBody CheckPlanPageQueryDTO queryDTO, @RequestBody Query query) {
		return R.data(planContentService.elevatorPlanHistory(queryDTO, query));
	}

	/**
	 * 查询电梯任务所有历史
	 *
	 * @param queryDTO
	 * @param query
	 * @return
	 */
	@GetMapping("/elevator/plan/history/all")
	public R<?> allElevatorPlanHistory(CheckPlanPageQueryDTO queryDTO, Query query) {
		return R.data(planContentService.allElevatorPlanHistory(queryDTO, Condition.getPage(query)));
	}

	/**
	 * 查询用户工单统计趋势
	 *
	 * @return
	 */
	@PostMapping("/user/check/plan/trend")
	public R<List<CheckPlanTrendVO>> selectUserCheckPlanTrend(@RequestBody CheckPlanTrendQueryDTO queryDTO) {
		return R.data(planUserDayCountService.selectUserCheckPlanTrend(queryDTO));
	}

	/**
	 * 查询单个用户统计图表
	 *
	 * @param userId
	 * @return
	 */
	@GetMapping("/check/plan/user/count")
	public R<CheckPlanUserCountVO> selectCheckPlanUserCount(Long userId, String startTime, String endTime, Integer useType) {
		return R.data(planUserDayCountService.selectCheckPlanCount(userId, startTime, endTime, useType));
	}

	/**
	 * 查询单个用户统计图表
	 *
	 * @param queryDTO
	 * @return
	 */
	@PostMapping("/check/plan/dept/count")
	public R<CheckPlanUserCountVO> selectCheckPlanDeptCount(@RequestBody CheckPlanDeptCountDTO queryDTO) {
		return R.data(planUserDayCountService.selectCheckPlanDeptCount(queryDTO));
	}

	/**
	 * 查询人员任务历史-保养
	 *
	 * @param queryDTO
	 * @return
	 */
	@PostMapping("/user/plan/history/by")
	@ApiOperation("查询人员任务历史-保养")
	public R<IPage<PlanDataPageVO>> elevatorPlanHistoryBy(@RequestBody ByPlanHistoryDTO queryDTO) {
		Query query = new Query();
		query.setCurrent(queryDTO.getCurrent());
		query.setSize(queryDTO.getSize());
		queryDTO.setUserId(AuthUtil.getUserId());
		if(queryDTO.getPlanBeginDate() == null){
			String beginTime = "2022-05-01 00:00:01";
			queryDTO.setPlanBeginDate(DateUtil.parse(beginTime,"yyyy-MM-dd HH:mm:ss"));
			queryDTO.setPlanEndDate(new Date());
		}
		return R.data(planContentService.elevatorPlanHistoryBy(queryDTO, Condition.getPage(query)));
	}

	/**
	 * 查询人员任务历史-维修
	 *
	 * @param queryDTO
	 * @param query
	 * @return
	 */
	@PostMapping("/user/plan/history/wx")
	public R<IPage<MalFunctionPlanPageVO>> elevatorPlanHistoryWx(@RequestBody WxPlanHistoryDTO queryDTO, @RequestBody Query query) {
		return R.data(planContentService.elevatorPlanHistoryWx(queryDTO, Condition.getPage(query)));
	}

	/**
	 * 查询人员任务历史-配件
	 *
	 * @param queryDTO
	 * @return
	 */
	@PostMapping("/user/plan/history/pj")
	@ApiOperation("查询人员任务历史-配件")
	public R<IPage<MalFunctionPlanPageVO>> elevatorPlanHistoryPj(@RequestBody PjPlanHistoryDTO queryDTO) {
		Query query = new Query();
		query.setCurrent(queryDTO.getCurrent());
		query.setSize(queryDTO.getSize());
		queryDTO.setUserId(AuthUtil.getUserId());
		return R.data(planContentService.elevatorPlanHistoryPj(queryDTO, Condition.getPage(query)));
	}

	/**
	 * 查询电梯任务完成历史-保养
	 *
	 * @param queryDTO
	 * @return
	 */
	@TenantIgnore
	@PostMapping("/elevator/plan/history/finish/by")
	public R<Map<String, Object>> elevatorPlanHistoryFinishBy(@RequestBody ByPlanHistoryDTO queryDTO) {
		return R.data(planContentService.elevatorPlanHistoryFinishBy(queryDTO));
	}

	/**
	 * 查询电梯任务未完成历史-保养
	 *
	 * @param queryDTO
	 * @return
	 */
	@TenantIgnore
	@PostMapping("/elevator/plan/history/unfinished/by")
	public R<Map<String, Object>> elevatorPlanHistoryUnfinishedBy(@RequestBody ByPlanHistoryDTO queryDTO) {
		return R.data(planContentService.elevatorPlanHistoryUnfinishedBy(queryDTO));
	}

	/**
	 * 查询电梯任务完成历史-维修
	 *
	 * @param queryDTO
	 * @return
	 */
	@PostMapping("/elevator/plan/history/finish/wx")
	public R<Map<String, Object>> elevatorPlanHistoryFinishWx(@RequestBody WxPlanHistoryDTO queryDTO) {
		return R.data(planContentService.elevatorPlanHistoryFinishWx(queryDTO));
	}

	/**
	 * 查询电梯任务未完成历史-维修
	 *
	 * @param queryDTO
	 * @return
	 */
	@PostMapping("/elevator/plan/history/unfinished/wx")
	public R<Map<String, Object>> elevatorPlanHistoryUnfinishedWx(@RequestBody WxPlanHistoryDTO queryDTO) {
		return R.data(planContentService.elevatorPlanHistoryUnfinishedWx(queryDTO));
	}

	/**
	 * 查询电梯任务完成历史-配件
	 *
	 * @param queryDTO
	 * @return
	 */
	@PostMapping("/elevator/plan/history/finish/pj")
	public R<Map<String, Object>> elevatorPlanHistoryFinishPj(@RequestBody PjPlanHistoryDTO queryDTO) {
		return R.data(planContentService.elevatorPlanHistoryFinishPj(queryDTO));
	}

	/**
	 * 查询电梯任务未完成历史-配件
	 *
	 * @param queryDTO
	 * @return
	 */
	@PostMapping("/elevator/plan/history/unfinished/pj")
	public R<Map<String, Object>> elevatorPlanHistoryUnfinishedPj(@RequestBody PjPlanHistoryDTO queryDTO) {
		return R.data(planContentService.elevatorPlanHistoryUnfinishedPj(queryDTO));
	}


	/**
	 * 根据工单id获取用户的执行信息
	 *
	 * @param planId
	 * @return
	 */
	@GetMapping("/getPlanUserInfo")
	@ApiOperation("根据工单id用户的执行情况")
	public R<Map<String, List<PlanUserInfoVo>>> getPlanUserInfo(Long planId) {
		return R.data(planContentService.getPlanUserInfo(planId));
	}


//	@Autowired
//	private IElevatorService elevatorService;
//	@Autowired
//	private IMaintainCheckHistoryService checkHistoryService;
//
//	@Autowired
//	private IUserService userService;
//
//	@Autowired
//	private IMaintainMalfunctionLogService maintainMalfunctionLogService;
//
//	/**
//	 * 获取任务详情
//	 * @param id
//	 * @return
//	 */
//	@GetMapping("/getPlanDetail")
//	public R<PlanDetailVo> getPlanDetail(String id,Long groupId,BladeUser user){
//		return R.data(planContentService.getPlanDetail(Long.valueOf(id),groupId, user.getUserId()));
//	}
//
//	/**
//	 * 修改电梯状态
//	 */
//	@PostMapping("/updateElevatorStatus")
//	public R<Boolean> updateElevatorStatus(@RequestBody Elevator elevator){
//		return R.status(elevatorService.update(new LambdaUpdateWrapper<Elevator>().set(Elevator::getIsStop,elevator.getIsStop()).eq(Elevator::getId,elevator.getId())));
//	}
//
//	/**
//	 * 修改任务预计时间
//	 * @param planContent
//	 * @return
//	 */
//	@PostMapping("/updatePlanFinishTime")
//	public R<Boolean> updatePlanFinishTime(@RequestBody MaintainPlanContent planContent){
//		User user = userService.getById(AuthUtil.getUserId());
//		MaintainMalfunctionLog log = new MaintainMalfunctionLog();
//		log.setPlanId(planContent.getId());
//		log.setTypeName(user.getRealName() + "将预计改为" + DateUtil.format(planContent.getFinishDate(),"yyyy-MM-dd HH:mm:ss") + "完成");
//		maintainMalfunctionLogService.save(log);
//		return R.status(planContentService.update(new LambdaUpdateWrapper<MaintainPlanContent>()
//			.set(MaintainPlanContent::getFinishDate,planContent.getFinishDate())
//			.eq(MaintainPlanContent::getId,planContent.getId())));
//	}
//
//	/**
//	 * 添加，重点详情或普通详情
//	 * @param maintainCheckHistoryList
//	 * @return
//	 */
//	@AutoIdempotent
//	@PostMapping("/addPlanCheckHistory")
//	public R<Boolean> addPlanCheckHistory(@RequestBody List<MaintainCheckHistory> maintainCheckHistoryList,BladeUser user){
//		return R.status(planContentService.savePlanCheckHistory(maintainCheckHistoryList,user.getUserId()));
//	}
//
//	/**
//	 * 修改，重点详情或普通详情
//	 * @param
//	 * @param maintainCheckHistory
//	 * @return
//	 */
//	@PutMapping("/updatePlanCheckHistory")
//	public R<Boolean> addPlanCheckHistory(@RequestBody MaintainCheckHistory maintainCheckHistory){
//		return R.status(checkHistoryService.updateById(maintainCheckHistory));
//	}
//
//	/**
//	 * 删除点检记录
//	 * @param id
//	 * @return
//	 */
//	@DeleteMapping("/removePlanCheckHistory/{id}")
//	public R<Boolean> removePlanCheckHistory(@PathVariable String id){
//		return R.status(checkHistoryService.removeById(Long.valueOf(id)));
//	}
//
//	/**
//	 * 保养完成
//	 * @param maintainPlanContent
//	 * @param user
//	 * @return
//	 */
//	@PostMapping("/accomplishCheck")
//	public R<Boolean> accomplishCheck(@RequestBody MaintainPlanContent maintainPlanContent, BladeUser user){
//		return R.status(planContentService.accomplishCheck(maintainPlanContent.getId(),user.getUserId()));
//	}
//
//	/**
//	 * 获取任务非重点检查所有项
//	 * @param id
//	 * @return
//	 */
//	@GetMapping("/getPlanPart")
//	public R<List<PlanCheckPartVo>> getPlanPart(String id,String name,BladeUser user){
//		return R.data(planContentService.getPlanPart(Long.valueOf(id),name,user.getUserId()));
//	}



	@PostMapping("/user/plan/history/repair")
	@ApiOperation("查询个人维修工单列表")
	@ApiOperationSupport(order = 0)
	@ApiImplicitParam(name = "queryDTO", value = "查询条件DTO")
	public R<IPage<MalFunctionPlanPageVO>> elevatorPlanHistoryRepair(@RequestBody RepairPlanHistoryDTO queryDTO) {
		Query query = new Query();
		query.setCurrent(queryDTO.getCurrent());
		query.setSize(queryDTO.getSize());
		queryDTO.setUserId(AuthUtil.getUserId());
		return R.data(planContentService.elevatorPlanHistoryRepair(queryDTO, Condition.getPage(query)));
	}

	/**
	 * 查询人员任务历史-保养(旧版)
	 *
	 * @param queryDTO
	 * @param query
	 * @return
	 */
	@PostMapping("/user/plan/history/by/skip")
	public R<IPage<PlanDataPageVO>> elevatorPlanHistoryBySkip(@RequestBody ByPlanHistorySkipDTO queryDTO, @RequestBody Query query) {
		return R.data(planContentService.elevatorPlanHistoryBySkip(queryDTO, Condition.getPage(query)));
	}


	/**
	 * 查询人员任务历史-配件(旧版)
	 *
	 * @param queryDTO
	 * @param query
	 * @return
	 */
	@PostMapping("/user/plan/history/pj/skip")
	public R<IPage<MalFunctionPlanPageVO>> elevatorPlanHistoryPjSkip(@RequestBody PjPlanHistorySkipDTO queryDTO, @RequestBody Query query) {
		return R.data(planContentService.elevatorPlanHistoryPjSkip(queryDTO, Condition.getPage(query)));
	}

}

































